//
//  AlbumViewController.m
//  Music
//
//  Created by mac on 16/4/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AlbumViewController.h"
#import "SonglistTableViewCell.h"
#import "SongModel.h"
#import "SongDetailViewController.h"


@interface AlbumViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong)  UIImageView *backImg;
@property (nonatomic , strong)  UIView *tableHeaderView;
@property (nonatomic , strong)  UIImageView *portrait_pic;
@property (nonatomic , strong)  UILabel *singerName;
@property (nonatomic , strong)  UILabel *nameLabel;
@property (nonatomic , strong)  UILabel *descLabel;
@property (nonatomic , strong)  UIView *naviView;
@property (nonatomic , strong)  UILabel *naviLabel;
@property (nonatomic , strong)  UIView *headerView;
@property (nonatomic , strong)  UILabel *songCount;
@property (nonatomic , strong)  NSString *songCountStr;
@property (nonatomic , strong)  UIButton *storeBtn;
@property (nonatomic , strong)  UITableView *tableView;
@property (nonatomic , strong)  NSArray *songArr;
@property (nonatomic , strong) UIView *loadingView;

@property (nonatomic , strong)  NSString *picUrl;
@end

@implementation AlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"yinyue"] style:UIBarButtonItemStyleDone target:self action:@selector(play:)];
    
    self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    self.naviView.backgroundColor = [UIColor colorWithRed:55/255.0 green:95/255.0 blue:205/255.0 alpha:1];
    _naviView.alpha = 0;
    self.naviLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    _naviLabel.textAlignment = NSTextAlignmentCenter;
    _naviLabel.textColor = [UIColor whiteColor];
    _naviLabel.font = [UIFont systemFontOfSize:22];
    [self.naviView addSubview:_naviLabel];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self createBackImg];
    [self createTableView];
    
    [self.view addSubview:_tableHeaderView];
    [self.view addSubview:_naviView];
    
    //[self createLoadingView];
    
    [self dataHandle];

}

-(void)play:(UIButton *)btn
{
    SongDetailViewController *sdVC = [SongDetailViewController new];
    [self presentViewController:sdVC animated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:(UIBarMetricsDefault)];
    self.navigationController.navigationBar.clipsToBounds = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:(UIBarMetricsDefault)];
    self.navigationController.navigationBar.clipsToBounds = NO;
}

- (void)dataHandle{
    NSString *strUrl = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@",self.albumId];
    
    [NetworkHandle GetDataWithURL:strUrl dic:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"data"];
        _naviLabel.text = dic[@"name"];
        _nameLabel.text = dic[@"name"];
        if ([dic[@"description"] isKindOfClass:[NSString class]]) {
            NSArray *temp = [dic[@"description"] componentsSeparatedByString:@"/r/n"];
            _descLabel.text = temp[0];
        }
        self.picUrl = dic[@"picUrl"];
        [_backImg sd_setImageWithURL:[NSURL URLWithString:dic[@"picUrl"]]];
        self.singerName.text = dic[@"singerName"];
        if (!dic[@"singerPicUrl"]) {
            [_portrait_pic sd_setImageWithURL:[NSURL URLWithString:dic[@"singerPicUrl"]]];
        }
        
        self.songCountStr = [NSString stringWithFormat:@"共%ld首歌曲",[dic[@"songs"] count]];
        NSArray *songArr = dic[@"songList"];
        self.songArr = [SongModel transformWithArray:songArr];
        [_tableView reloadData];
        //[self.tkLoadingAnimationView stopAnimating];
        [self.loadingView removeFromSuperview];
    } filed:^(NSError *error) {
    }];
}

- (void)createBackImg{
    self.backImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, -30, self.view.frame.size.width, 150+65+64+30+30)];
        _backImg.backgroundColor = [UIColor clearColor];
    _backImg.image = [UIImage imageNamed:@"background"];
    [self.view addSubview:_backImg];

}

- (void)createTableView{
    [self createTableViewHeaderView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:(UITableViewStylePlain)];
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    tempView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = tempView;
    [self.tableView registerClass:[SonglistTableViewCell class] forCellReuseIdentifier:@"songlistCell"];
    [self.view addSubview:self.tableView];
  
}

- (void)createTableViewHeaderView{
    self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150 + 64)];
    _tableHeaderView.backgroundColor = [UIColor clearColor];
    
    self.portrait_pic = [[UIImageView alloc] init];
    _portrait_pic.frame = CGRectMake(15, 64+5, 35, 35);
    _portrait_pic.backgroundColor = [UIColor clearColor];
    _portrait_pic.layer.cornerRadius = 35/2.0;
    _portrait_pic.clipsToBounds = YES;
    [_tableHeaderView addSubview:_portrait_pic];
    
//    self.singerName = [[UILabel alloc] initWithFrame:CGRectMake(_portrait_pic.frame.origin.x + _portrait_pic.frame.size.width + 5, 0, 100, 30)];
//    _singerName.center = CGPointMake(_singerName.center.x, _portrait_pic.center.y);
//    _singerName.font = [UIFont systemFontOfSize:14];
//    _singerName.textColor = [UIColor whiteColor];
//    _singerName.shadowOffset = CGSizeMake(1, 1);
//    _singerName.shadowColor = [UIColor blackColor];
//    _singerName.backgroundColor = [UIColor clearColor];
//    [_tableHeaderView addSubview:_singerName];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_portrait_pic.frame.origin.x , _portrait_pic.frame.origin.y + _portrait_pic.frame.size.height + 20, 200, 35)];
    _nameLabel.font = [UIFont systemFontOfSize:20];
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.shadowOffset = CGSizeMake(1, 1);
    _nameLabel.shadowColor = [UIColor blackColor];
    _nameLabel.backgroundColor = [UIColor clearColor];
    [_tableHeaderView addSubview:_nameLabel];
    
//    self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 10, _tableHeaderView.frame.size.width - _nameLabel.frame.origin.x * 2, 35)];
//    _descLabel.font = [UIFont systemFontOfSize:14];
//    _descLabel.textColor = [UIColor whiteColor];
//    _descLabel.shadowOffset = CGSizeMake(1, 1);
//    _descLabel.shadowColor = [UIColor blackColor];
//    _descLabel.backgroundColor = [UIColor clearColor];
//    [_tableHeaderView addSubview:_descLabel];
    
 
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.songArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SonglistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songlistCell" forIndexPath:indexPath];
    SongModel *songModel = _songArr[indexPath.row];
    cell.numLabel.text = [@(indexPath.row + 1) stringValue];
    cell.songName.text = songModel.name;
    cell.singerName.text = songModel.singerName;
    NSInteger count = [songModel.favorites integerValue];
    NSString *str = nil;
    if (count >= 10000) {
        CGFloat num = count / 10000.0;
        if (count % 10000 >= 1000) {
            str = [NSString stringWithFormat:@"%.1f 万",num];
        } else {
            str = [NSString stringWithFormat:@"%.0f 万",num];
        }
    } else {
        str = [NSString stringWithFormat:@"%ld",count];
    }
    cell.favorites.text = str;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 65)];
    _headerView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.6];
    self.songCount = [[UILabel alloc] initWithFrame:CGRectMake(20, 7.5, 200, 50)];
    _songCount.backgroundColor = [UIColor clearColor];
    _songCount.textColor = [UIColor whiteColor];
    _songCount.text = _songCountStr;
    [_headerView addSubview:_songCount];
    
        return _headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 65;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _tableHeaderView.frame = CGRectMake(0, -scrollView.contentOffset.y, _tableHeaderView.frame.size.width, _tableHeaderView.frame.size.height);
    if (scrollView.contentOffset.y <= 0 && scrollView.contentOffset.y >= -60) {
        _backImg.frame = CGRectMake(0, -30 - (scrollView.contentOffset.y / 2.0), _backImg.frame.size.width, _backImg.frame.size.height);
    }
    if (scrollView.contentOffset.y <= -60) {
        CGFloat h = 339+(-scrollView.contentOffset.y -60);
        CGFloat scale = h / 339;
        CGFloat x = -(self.view.frame.size.width * scale - self.view.frame.size.width) / 2;
        CGFloat w = self.view.frame.size.width *scale;
        _backImg.frame = CGRectMake(x, _backImg.frame.origin.y, w, h);
    }
    if (scrollView.contentOffset.y >= 0) {
        _backImg.frame = CGRectMake(0, -30 - scrollView.contentOffset.y, _backImg.frame.size.width, _backImg.frame.size.height);
        CGFloat alpha = 0.6 + 0.4 * scrollView.contentOffset.y / 150.0;
        _headerView.backgroundColor = [_headerView.backgroundColor colorWithAlphaComponent:alpha];
        _naviView.alpha = scrollView.contentOffset.y / 150.0;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y >= 0) {
        _naviView.alpha = scrollView.contentOffset.y / 150.0;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [NOTIFICATION_CENTER postNotificationName:@"playSongs" object:self.songArr userInfo:@{@"type":@"Song",@"indexPath":indexPath}];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
